{# Stalker Pyramid a Web Base Production Asset Management System
 Copyright (C) 2009-2014 Erkan Ozgur Yilmaz
 
 This file is part of Stalker Pyramid.
 
 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation;
 version 2.1 of the License.

 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#}
<div class='dijitDialogPaneContentArea'>
    <form id='status_list_form'>
        <table style='width: 100%'>

            {# NAME #}
            <tr>
                <td class='label_column'>
                    <label for='status_list_name'>
                        Name
                    </label>
                </td>
                <td class='input_column'>
                    <input id='status_list_name'>
                </td>
            </tr>

            {# TARGET ENTITY TYPE #}
            <tr>
                <td class='label_column'>
                    <label for='status_list_target_entity_type'>
                        Target Entity Type
                    </label>
                </td>
                <td class='input_column'>
                    <input id='status_list_target_entity_type'>
                </td>
            </tr>

            {# STATUSES #}
            <tr>
                <td class='label_column'>
                    <label for='status_list_statuses'>
                        Statuses
                    </label>
                </td>
                <td class='input_column'>
                    <select id='status_list_statuses'></select>
                </td>
                <td class='button_column'>
                    <button id='status_list_create_status_button'>New</button>
                    {#            <button id='status_list_update_status_button'>Update</button>#}
                </td>
            </tr>

        </table>
    </form>
</div>

<div class='dijitDialogPaneActionBar'>
    <button id='status_list_ok_button'></button>
    <button id='status_list_cancel_button'></button>
</div>

<script type='text/javascript'>
    require([
        'dojo/store/Memory',
        'dojo/store/JsonRest',
        
        'dijit/form/Form',
        'dijit/form/ValidationTextBox',
        'dijit/form/Button',
        'dijit/form/FilteringSelect',
        
        'stalker/js/dialogs',
        'stalker/js/TagSelect',
        'stalker/js/submitForm',
        'stalker/js/fieldUpdater',
        'stalker/js/dialogCaller',
        
        'dojo/domReady!'
    ], function (Memory, JsonRest, Form, ValidationTextBox,
                 Button, FilteringSelect, dialogs, TagSelect,
                 submitForm, fieldUpdater, dialogCaller) {
        // ********************************************************************
        // Generate some default values
        var name;
        var name_disabled = false;
        var target_entity_type;
        var target_entity_type_disabled = false;
        var url;
        var additional_data = {};
        var selected_statuses = [];
        
        {% if mode=='CREATE' %}
            url = '{{ request.route_url('create_status_list') }}';
            {% if target_entity_type %}
                name = '{{ target_entity_type }} Statuses';
                name_disabled = true;
                target_entity_type = '{{ target_entity_type }}';
                target_entity_type_disabled = true;
            {% endif %}
        {% elif mode=='UPDATE' %}
            name = '{{ status_list.name }}';
            name_disabled = true;
            target_entity_type = '{{ status_list.target_entity_type }}';
            target_entity_type_disabled = true;
            selected_statuses = [
                {% for status in status_list.statuses %}
                    {{ status.id }},
                {% endfor %}
            ];
            
            url = '{{ request.route_url('update_status_list', id=status_list.id) }}';
            additional_data = {
{#                'status_list_id': {{ status_list.id }}#}
            };
        {% endif %}
        
        // ********************************************************************
        // Form
        var status_list_form = new Form({}, 'status_list_form');

        var dialog = status_list_form.getParent();

        // ********************************************************************
        // Name
        var name_textBox = new ValidationTextBox({
            name: 'name',
            label: 'Name',
            placeHolder: 'Enter a name',
            required: true,
            disabled: name_disabled,
            value: name
        }, 'status_list_name');

        // ********************************************************************
        // Target Entity Type
        var target_entity_type_store = new Memory({
            data: [
                {% for entity_type in entity_types %}
                    {
                        name: '{{ entity_type.name }}',
                        id: '{{ entity_type.name }}'
                    },
                {% endfor %}
            ]
        });

        var target_entity_type_select = new FilteringSelect({
            name: 'target_entity_type',
            label: 'Target Entity Type',
            required: true,
            placeHolder: 'Select A Target Entity Type',
            store: target_entity_type_store,
            disabled: target_entity_type_disabled,
            value: target_entity_type
        }, 'status_list_target_entity_type');


        // ********************************************************************
        // Statuses
        // 
        // The Memory
        var statuses_memory = new JsonRest({
            target: '{{ request.route_url('get_statuses') }}'
        });

        // The Widget
        var statuses_tagSelect = new TagSelect({
            name: 'status_ids',
            required: true
        }, 'status_list_statuses');


        var set_statuses_tagSelect_value = function(){
            statuses_tagSelect.set('value', selected_statuses);
        }

        // The Updater
        var statuses_field_updater = fieldUpdater({
            'memory': statuses_memory,
            'widget': statuses_tagSelect,
            //'selected': selected_statuses
            'callBack':set_statuses_tagSelect_value
        });
        // run the function to fill the data for the first time
        statuses_field_updater({animate: false});

        // New Status Button
        var create_status_button = dialogCaller({
            label: 'New',
            dialog_id: 'status_dialog',
            content_creator: dialogs.create_status_dialog,
            attach_to: 'status_list_create_status_button',
            related_field_updater: statuses_field_updater,
            data_id: function () {
                selected_statuses = statuses_tagSelect.get('value');
                return selected_statuses;
            }
        });


        // ********************************************************************
        // Ok Button
        var ok_button = new Button({
            label: 'OK',
            type: 'button',
            disabled: false,
            onClick: function () {
                name_textBox.set('disabled', false);
                target_entity_type_select.set('disabled', false);
                submitForm({
                    dialog: dialog,
                    form: status_list_form,
                    additional_data: additional_data,
                    url: url,
                    method: 'POST'
                });
            }
        }, 'status_list_ok_button');


        // ********************************************************************
        // Cancel Button
        var cancel_button = new Button({
            label: 'Cancel',
            type: 'button',
            onClick: function () {
                dialog.destroyRecursive();
            }
        }, 'status_list_cancel_button');


        // Startups
        target_entity_type_select.startup();
        name_textBox.startup();
        statuses_tagSelect.startup();
        create_status_button.startup();
        ok_button.startup();
        cancel_button.startup();
        status_list_form.startup();

    });
</script>

